package cn.aifeix.org.controller;

import cn.aifeix.basic.anno.LogAnno;
import cn.aifeix.basic.util.AjaxResult;
import cn.aifeix.basic.util.PageList;
import cn.aifeix.org.domain.DictionaryType;
import cn.aifeix.org.query.DictionaryTypeQuery;
import cn.aifeix.org.service.DictionaryTypeService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dic")
public class DictionaryTypeController {
    @Autowired
    private DictionaryTypeService dictionaryTypeService;

    /**
     * 添加/修改
     */
    @PutMapping
    @LogAnno(method = "addOrUpdate",methodDes = "添加或修改")
    public AjaxResult addOrUpdate(@RequestBody DictionaryType dictionaryType){
        try {
            if (null == dictionaryType.getId()){
                dictionaryTypeService.add(dictionaryType);
            }else {
                dictionaryTypeService.update(dictionaryType);
            }
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    @LogAnno(method = "delete",methodDes = "删除")
    public AjaxResult delete(@PathVariable("id") Long id){
        dictionaryTypeService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 查询所有
     */
    @GetMapping
    @LogAnno(method = "findAll",methodDes = "查询所有")
    public List<DictionaryType> findAll(){
        return dictionaryTypeService.findAll();
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("/{id}")
    public DictionaryType findById(@ApiParam(value="ID属性",required = true) @PathVariable("id") Long id){
        return dictionaryTypeService.findById(id);
    }
    /**
     * 批量删除
     */
    @PatchMapping
    @LogAnno(method = "batchDel",methodDes = "批量删除")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        dictionaryTypeService.batchDel(ids);
        return AjaxResult.me().success();
    }

    /**
     *
     * 分页查询
     */
    @PostMapping
    public PageList<DictionaryType> queryByPage(@RequestBody DictionaryTypeQuery query){
        return dictionaryTypeService.queryByPage(query);
    }


}
